<?php

namespace catchAdmin\stock\model;

use catchAdmin\stock\model\search\InventorySearch;
use catcher\base\CatchModel;
use think\facade\Db;
/**
 *
 * @property int $id
 * @property string $inventory_id
 * @property string $config_id
 * @property int $number
 * @property int $box_number
 * @property int $isstatus
 * @property int $createtime
 * @property int $updatetime
 * @property string $status
 * @property int $disc_surplus
 * @property int $disc_deficit
 * @property string $remarks
 * @property int $admin_id
 * @property int $sort
 * @property int $total_number
 */
class Inventory extends CatchModel
{
    use InventorySearch;
    
    public $field = [
        // ID
        'id',
        // 盘点编号
        'inventory_id',
        // 库位编号(关联库位表）
        'config_id',
        // 数量
        'number',
        // 箱数
        'box_number',
        // 盘点状态:0=盘点中,1=完结
        'status',
        // 总盘盈
        'disc_surplus',
        // 总盘亏
        'disc_deficit',
        // 备注
        'remarks',
        // 盘点人
        'auth_id',
        // 总实盘数量
        'total_number',
         // 创建时间
        'created_at',
        // 更新时间
        'updated_at',
        // 删除状态，0未删除 >0 已删除
        'deleted_at',
        // 商户id
        'tenant_id',
        // 创建者ID
        'creator_id',
        'inventory_type',
        'goods_ids',
        'start_datetime',
        'end_datetime',
        'is_all_product'
    ];
    
    public $name = 'db_inventory';

    /**
     *
     *
     * @time 2019年12月08日
     * @return array|\think\Paginator
     *@throws \think\db\exception\DbException
     */
    public function getList($tenant_id=0)
    {
        $where = [];
        if($tenant_id !== 0){
            $where[]=['tenant_id','=',$tenant_id];
        }
        return $this->withoutField(['updated_at'], true)
            ->catchSearch()
            ->where($where)
            ->withoutField('tenant_id,creator_id,updated_at,deleted_at')
            ->order($this->aliasField('id'), 'desc')
            ->paginate()
            ->each(function (&$item){
//                $item['config_name'] = Db::table('db_warehouse_config')
//                    ->where(['config_id' => $item['config_id']])
//                    ->value('name');
                if(!empty($item['config_id'])){
                    $item['config_name'] = (new WarehouseConfig)
                        ->where('config_id',$item['config_id'])->find()->full_name;
                }
                $item['user_name'] = Db::table('users')
                    ->where(['id' => $item['auth_id']])
                    ->value('username');

                $item['start_datetime'] = date('Y-m-d',strtotime($item['start_datetime']));
                $item['end_datetime'] = date('Y-m-d',strtotime($item['end_datetime']));

                $item['accuracy_rate'] = $item['total_number'] == 0 ? '--'  :  (float)$item['accuracy_rate'] * 100 . "%";
            });
    }

    public static function onBeforeInsert($model){
        if(1 != $model->inventory_type){
            $model->goods_ids = '';
        }

//        if(2 == $model->inventory_type){
//            $model->start_datetime = time();
//            $model->end_datetime = time();
//        }

    }
}